package com.geese.module.user.controller;

import com.geese.common.constants.BusinessConstants;
import com.geese.common.entity.Permission;
import com.geese.exception.BusinessException;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.geese.common.BaseController;
import com.geese.module.security.service.PermissionService;
import com.geese.module.user.model.SecurityUser;
import com.geese.module.user.service.UserService;
import com.google.common.collect.ImmutableMap;

/**
 * @author YeFangYi 
 */
@Controller
@Scope("prototype")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * CEO登录
     */
    @PostMapping(value = "/api/ceo/auth")
    @ResponseBody
    public String ceoLogin(String account, String password) {
    	if(StringUtils.isEmpty(account)) {
    		throw new BusinessException(120001);
    	}
    	if(StringUtils.isEmpty(password)) {
    		throw new BusinessException(120001);
    	}
        ImmutableMap result = userService.ceoLogin(account, password);
        return response(result);
    }
    
    /**
     * 企业登录
     */
    @PostMapping(value = "/api/admin/auth")
    @ResponseBody
    public String adminLogin(String account, String password) {
    	if(StringUtils.isEmpty(account)) {
    		throw new BusinessException(120001);
    	}
    	if(StringUtils.isEmpty(password)) {
    		throw new BusinessException(120001);
    	}
        return response(userService.adminLogin(account, password));
    }

    /**
     * 商家登录
     */
    @PostMapping(value = "/api/business/auth")
    @ResponseBody
    public String businessLogin(String account, String password) {
        if(StringUtils.isEmpty(account)) {
            throw new BusinessException(120001);
        }
        if(StringUtils.isEmpty(password)) {
            throw new BusinessException(120001);
        }
        String token = userService.businessLogin(account, password);
        return response(ImmutableMap.of("token", token));
    }

    /**
     * 小二登录
     */
    @PostMapping(value = "/api/courier/auth")
    @ResponseBody
    public String courierLogin(String account, String password) {
        if(StringUtils.isEmpty(account)) {
            throw new BusinessException(120001);
        }
        if(StringUtils.isEmpty(password)) {
            throw new BusinessException(120001);
        }
        String token = userService.courierLogin(account, password);
        return response(ImmutableMap.of("token", token));
    }


    /**
     * 退出，token放入黑名单
     */
    @GetMapping(value = "/api/logout")
    @ResponseBody
    public String lougout(@RequestParam String token) {
        userService.logout(token);
        return response();
    }

    /**
     * 修改密码
     */
    @PostMapping(value = "/api/changepwd")
    @ResponseBody
    public String changePwd(String oldPwd, String newPwd, String token) {
    	
    	if(StringUtils.isEmpty(oldPwd)) {
    		 throw new BusinessException(120056);
    	}
    	if(StringUtils.isEmpty(newPwd)) {
			 throw new BusinessException(120057);
		}
    	
        SecurityUser user = userService.getLoginUser(token);

        userService.updatePwd(user.getUserId(), oldPwd, newPwd);
        return response();
    }

}
